package ruc.DolphinDB.read;

import com.xxdb.DBConnection;
import com.xxdb.data.Entity;
import ruc.Consts.constant;
import ruc.DolphinDB.DolphinDB_Common;
import ruc.utils.IOUtils;
import ruc.utils.Pval;

import java.io.IOException;
import java.util.*;

public class DolphinDB_SingleThreadRead {
    private int port;
    private String IP, tbName;
    public DolphinDB_SingleThreadRead(String IP, int port, String tbName){
        this.IP = IP;
        this.port = port;
        this.tbName = tbName;
    }

    // p50、p90、p99
    void execute(String eSql, int type) throws IOException {
        long arr[] = new long[100];
        long tol = 0;
        DBConnection conn = DolphinDB_Common.getDBConnection(IP, port);
        HashMap<Integer, List<Long>> m = new HashMap<>();
        for(int i = 0; i < 10000; i++) {
            long st_time = System.nanoTime();
            Entity entity = conn.run(eSql);
            System.out.println(entity.getString());
            long diff = (System.nanoTime() - st_time) / constant.Ns2Ms;
            int key = Pval.check(diff); // 分桶
            if (m.containsKey(key)) {
                m.get(key).add(diff);
            } else {
                List<Long> l = new ArrayList<>();
                l.add(diff);
                m.put(key, l);
            }
            tol += diff;
        }
        conn.close();


        System.out.printf("[TSBench-D] [TDolphinDB_query%d] >>>>> avg Response time：%d ms, pc50：%d ms, pc90:%d ms, pc99: %d ms >>>>>> \n", type, tol / 10000, Pval.calPVal(m, 50), Pval.calPVal(m, 90), Pval.calPVal(m, 99));
        IOUtils.WriteData(constant.GetWriteFilePath("DolphinDB", "SingleThreadRead"),
                new String[]{String.valueOf(System.nanoTime()), String.valueOf(type), String.valueOf(tol / 10000), String.valueOf(Pval.calPVal(m, 50)), String.valueOf(Pval.calPVal(m, 90)), String.valueOf(Pval.calPVal(m, 99))});
    }

    // select max(cpu_usage) from TSBenchD_Concurrent where hostname = 'dde8784e-6f96-11ec-bd43-cd6282b60352' and server = 'SYS.VPN' and ts >= 2021.08.25 15:47:00 and ts <= 2021.09.03 12:30:00 group by minute(ts)
    public void query1(String server, String hostName, long st, long ed) throws IOException {
        String format = "select max(cpu_usage) from %s where hostname = '%s' and server = '%s' and ts >= %s and ts <= %s group by minute(ts)";
        String eSQL = String.format(format, tbName, hostName, server,DolphinDB_Common.DataUtil(st), DolphinDB_Common.DataUtil(ed));
        execute(eSQL, 1);
    }

    // select max(cpu_usage), mean(memory_usage), sum(disk_usage) from TSBenchD_Concurrent where (server = 'AGT.ECS' or server = 'SYS.DDS' or server = 'SYS.SFS' or server = 'SYS.AS') and ts >= 2021.08.25 15:47:00 and ts <= 2021.09.03 12:30:00 group by hour(ts
    public void query2(String s1, String s2, String s3, String s4, long st, long ed) throws IOException {
        String format = "select max(cpu_usage), mean(memory_usage), sum(disk_usage) from %s where (server = '%s' or server = '%s' or server = '%s' or server = '%s') and ts >= %s and ts <= %s group by hour(ts)";
        String eSQL = String.format(format, tbName, s1, s2, s3, s4, DolphinDB_Common.DataUtil(st), DolphinDB_Common.DataUtil(ed));
        execute(eSQL, 2);
    }

    // select max(disk_usage) from TSBenchD_Concurrent where ts >= 2021.08.25 15:47:00 and ts <= 2021.09.03 12:30:00 group by hour(ts) as time order by time desc
    public void query3(long st, long ed) throws IOException {
        String format = "select max(disk_usage) from %s where ts >= %s and ts <= %s group by hour(ts) as time order by time desc";
        String eSQL = String.format(format, tbName, DolphinDB_Common.DataUtil(st), DolphinDB_Common.DataUtil(ed));
        execute(eSQL, 3);
    }

    public void query4(long st, long ed) throws IOException {
        String format = "select max(cpu_usage) from %s where ts >= %s and ts <= %s group by hour(ts), hostname";
        String eSQL = String.format(format, tbName, DolphinDB_Common.DataUtil(st), DolphinDB_Common.DataUtil(ed));
        execute(eSQL, 4);
    }

    public static void main(String[] args) throws IOException {
        DolphinDB_SingleThreadRead db = new DolphinDB_SingleThreadRead(constant.IP, constant.DolphinDB_Port, constant.Concurrent_tbName);
        String []serverList = constant.server;
        Random ran = new Random();

        db.query1(serverList[ran.nextInt(serverList.length)], "dde8784e-6f96-11ec-bd43-cd6282b60352", 1629877620000L, 1630643400000L);
        db.query2(serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], serverList[ran.nextInt(serverList.length)], 1629877620000L, 1630643400000L);
        db.query3(1629877620000L, 1630643400000L);
        db.query4(1629877620000L, 1630643400000L);
    }
}
